<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC1014 – Use `if cmd; then ..` to check exit code, or `if [ "$(cmd)" = .. ]` to check output.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC1014 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC1014">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="use-if-cmd-then--to-check-exit-code-or-if--cmd----to-check-output">Use
<code>if cmd; then ..</code> to check exit code, or
<code>if [ "$(cmd)" = .. ]</code> to check output.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC1014.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="bu">[</span> grep -q pattern <span class="er">file</span> <span class="ex">]</span></span>
<span id="cb1-2"><a href="SC1014.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb1-3"><a href="SC1014.html#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Found a match&quot;</span></span>
<span id="cb1-4"><a href="SC1014.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC1014.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="fu">grep</span> <span class="at">-q</span> pattern file</span>
<span id="cb2-2"><a href="SC1014.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb2-3"><a href="SC1014.html#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Found a match&quot;</span></span>
<span id="cb2-4"><a href="SC1014.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p><code>[ .. ]</code> is not part of shell syntax like <code>if</code>
statements. It is not equivalent to parentheses in C-like languages,
<code>if (foo) { bar; }</code>, and should not be wrapped around
commands to test.</p>
<p><code>[</code> is just regular command, like <code>whoami</code> or
<code>grep</code>, but with a funny name (see
<code>ls -l /bin/[</code>). It's a shorthand for <code>test</code>.</p>
<p>If you want to check the exit status of a certain command, use that
command directly as demonstrated in the correct code.</p>
<p>If you want to check the output of a command, use
<code>"$(..)"</code> to get its output, and then use <code>test</code>
or <code>[</code>/<code>[[</code> to do a string comparison:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC1014.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Check output of `whoami` against the string `root`</span></span>
<span id="cb3-2"><a href="SC1014.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="bu">[</span> <span class="st">&quot;</span><span class="va">$(</span><span class="fu">whoami</span><span class="va">)</span><span class="st">&quot;</span> <span class="ot">=</span> <span class="st">&quot;root&quot;</span> <span class="bu">]</span></span>
<span id="cb3-3"><a href="SC1014.html#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb3-4"><a href="SC1014.html#cb3-4" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Running as root&quot;</span></span>
<span id="cb3-5"><a href="SC1014.html#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<p>For more information, see <a
href="http://mywiki.wooledge.org/BashPitfalls#if_.5Bgrep_foo_myfile.5D">this
problem in the Bash Pitfall</a> list, or generally <a
href="http://mywiki.wooledge.org/BashGuide/TestsAndConditionals">Tests
and Conditionals</a> in the WoolEdge BashGuide</p>
<h3 id="exceptions">Exceptions:</h3>
<p>None.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a
href="https://stackoverflow.com/questions/2480584/how-do-i-use-a-file-grep-comparison-inside-a-bash-if-else-statement">How
do I use a file grep comparison inside a bash if/else
statement?</a></li>
<li><a
href="https://mywiki.wooledge.org/BashPitfalls#if_.5Bgrep_foo_myfile.5D">Bash
Pitfalls: <code>if [grep foo myfile]</code></a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


